﻿using System;
using Ninject;

namespace Codeflows.Contrib.Ninject
{
    public class NinjectCodeflowFactory : ICodeflowFactory
    {
        private readonly IKernel _kernel;

        public NinjectCodeflowFactory(IKernel kernel)
        {
            _kernel = kernel;
        }

        public DisposableContext<Codeflow> CreateCodeflow(Type codeflowType)
        {
            var codeFlow = (Codeflow)_kernel.Get(codeflowType);
            
            return new DisposableContext<Codeflow>(codeFlow, instance => _kernel.Release(instance));
        }
    }
}
